package com.projecy.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.projecy.common.constant.CommonConstants;
import com.projecy.common.exception.BadRequestException;
import com.projecy.system.mapper.SysRoleMapper;
import com.projecy.system.mapper.SysRoleMenuMapper;
import com.projecy.system.model.SysRole;
import com.projecy.system.model.SysRoleMenu;
import com.projecy.system.model.dto.RoleMenuDto;
import com.projecy.system.service.SysRoleMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;


@Service
@RequiredArgsConstructor
public class SysRoleMenuServiceImpl implements SysRoleMenuService {
    @Autowired
    SysRoleMenuMapper roleMenuMapper;
    @Autowired
    SysRoleMapper roleMapper;


    @Override//通过角色ID获取对应的菜单
    public List<SysRoleMenu> getMenuByRoleId(Long roleId) {
        System.out.println("通过角色ID获取对应的菜单");
        try {
            LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysRoleMenu::getRoleId, roleId);
            return roleMenuMapper.selectList(wrapper);
        } catch (BadRequestException e) {
            e.printStackTrace();
            throw new BadRequestException(e.getMsg());
        }
    }


    @Override
    @Transactional//授权角色菜单
    public void editMenuRoleByRoleId(RoleMenuDto roleMenuDto) {
        System.out.println("授权角色菜单");
        try {
            Long roleId = roleMenuDto.getRoleId();
            //  获取当前角色信息
            SysRole role = roleMapper.selectById(roleId);
            if (CommonConstants.ROLE_ADMIN.equals(role.getRoleCode())){
                throw new BadRequestException("超级管理员拥有所有权限，无需授权");
            }
            //  1先清空之前角色的所有菜单
            deleteByRoleId(roleId);
            //  2遍历所有的菜单
            if (!CollectionUtils.isEmpty(roleMenuDto.getMenuIds())){
                roleMenuDto.getMenuIds().forEach(menuId -> {
                    SysRoleMenu roleMenu = new SysRoleMenu();
                    roleMenu.setMenuId(menuId);
                    roleMenu.setRoleId(roleId);
                    roleMenuMapper.insert(roleMenu);
                });
            }
        } catch (BadRequestException e) {
            e.printStackTrace();
            throw new BadRequestException(e.getMsg());
        }
    }


    @Override
    public void deleteByRoleId(Long roleId) {//删除与角色绑定的菜单
        System.out.println("删除与角色绑定的菜单");
        try {
            LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysRoleMenu::getRoleId, roleId);
            roleMenuMapper.delete(wrapper);
        } catch (BadRequestException e) {
            e.printStackTrace();
            throw new BadRequestException(e.getMsg());
        }
    }
}
